package de.tum.in.eist.im.client;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.im.client.domain.User;
import de.tum.in.eist.im.client.domain.UserEvent;

/**
 * @author Christian Ittner
 */
public class UserListView {  
	private ChatServiceAsync chatService;
	private List<User> userList;

	private CellList<String> userListWidget;
	private VerticalPanel userListPanel;

	/**
	 * Initializes the UserList.
	 */
	public UserListView(ChatServiceAsync chatSvc) {
		this.chatService = chatSvc;
		userList = new LinkedList<User>();
		userListWidget = new CellList<String>(new TextCell());
		userListPanel = new VerticalPanel();
		userListPanel.addStyleName("user-list");
		userListPanel.setSpacing(8);
		userListPanel.add(new Label("User List"));
		userListPanel.add(userListWidget);
		
		this.chatService.getLoggedInUsers(new AsyncCallback<List<User>>() {
			public void onFailure(Throwable exception) { /* error connecting to server */}
			public void onSuccess(List<User> loggedInUsers) {
				userList=loggedInUsers;
				List<String> userNames = new LinkedList<String>();
				userNames.add("Currently online: "+Integer.toString(userList.size())+" User(s)");
				for (User user: userList) { userNames.add(user.toString());}
				userListWidget.setRowData(0, userNames);
			}
		});
	}

	/**
	 * @return provides userList-Panel to be displayed on website.
	 */	
	public  VerticalPanel getUserListWidget() {
		
		return userListPanel;
	}
	
	/**
	 * Updates the User List 
	 * @param userEvents List of login/logout-Events e.g. provided by ServerPush
	 * @return returns updated userList-Panel.
	 */	
	public VerticalPanel updateUserList(List<UserEvent> userEvents) {
		//process userevents
		
		return getUserListWidget();
		
	}

}